package web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ServletIntitalizePwd
 */
@WebServlet(description = "初始化学院账号的密码", urlPatterns = { "/ServletIntitalizePwd","/Manager/Intitalize","/changepwd"})
public class ServletIntitalizePwd extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    request.setCharacterEncoding("utf-8");
	    String UserNum = request.getParameter("usernum");
	    String pg = request.getParameter("pg");
	    String md5pwd=MD5.md5("123456");
	    String sql = "";
	    //构造写入数据库的SQL
	   
	     //更新用户表的密码
	     sql = "update user set UserPwd='"+md5pwd+"' where UserNum='"+UserNum+"'";  
	     System.out.println("UserUpdate sql=" + sql);

	    //连接数据库
			try {
				int ret;
				ret = new DBCon().query(sql);
		        System.out.println("ret=" + ret);
		        System.out.println("学院密码初始化成功");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    //返回页面
		request.setAttribute("p",pg);
		System.out.println("Sendback pg="+pg);
		//重定向
	    request.getRequestDispatcher("DepEdit.jsp").forward(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		request.setCharacterEncoding("utf-8");
		String TeaId = request.getParameter("TeaId");
		String NewPwd = request.getParameter("NewPwd");
		String ReNewPwd = request.getParameter("ReNewPwd");
		//构造写入数据库sql

		  String  sql = "update user set UserPwd='"+NewPwd+"' where  UserNum='"+TeaId+"'";			
		System.out.println("sql="+sql);
		 int r = 0;
		try {
			if (sql.length()>0)
			r = new DBtools().execute(sql);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 System.out.println("影响行数："+r); 		 
		//返回页面
		response.sendRedirect("/loginout.jsp");
			
	}

}
